﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraLayout;
using Kevin.WindosControls.Test.Entity;
using Kevin.Framework.Core;
using System.Reflection;

namespace Kevin.WindosControls.Test
{
    public partial class FrmChild : Form, IDragManager
    {
        

        public FrmChild()
        {
            InitializeComponent();
            Init();
        }

        private FormsInfo FormSetting
        {
            get;
            set;
        }

        private List<InputItemInfo> Inputs = new List<InputItemInfo>();
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            this.listView1.Items.Clear();
            this.listView1.Items.Add(new ListViewItem() { Name="id",Text="编码",  });
            this.listView1.Items.Add(new ListViewItem() { Name = "name", Text = "名称" });
            this.listView1.Items.Add(new ListViewItem() { Name = "layperson", Text = "法人" });
            this.listView1.Items.Add(new ListViewItem() { Name = "link", Text = "地址" });
            Inputs.Add(new InputItemInfo() { ID = "id", Text = "编码", CtrlType = "TextBox" });
            Inputs.Add(new InputItemInfo() { ID = "name", Text = "名称", CtrlType = "TextBox" });
            Inputs.Add(new InputItemInfo() { ID = "layperson", Text = "法人", CtrlType = "ComboBox" });
            Inputs.Add(new InputItemInfo() { ID = "link", Text = "地址", CtrlType = "TextBox" });
            //1：新增时需知道表名称，然后从表中获取表结构信息
            if (FormSetting == null)
            {
                return;
            }

            //2：载入时直接从设计器持久化数据中获取数据
        }

        //设置鼠标样式
        private void SetDragCursor(DragDropEffects e)
        {
            return;

            if (e == DragDropEffects.Move)
                Cursor = new Cursor(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("DevExpress.XtraLayout.Demos.Images.move.ico"));
            if (e == DragDropEffects.Copy)
                Cursor = new Cursor(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("DevExpress.XtraLayout.Demos.Images.copy.ico"));
            if (e == DragDropEffects.None)
                Cursor = Cursors.No;
        }

        #region Events

        //ListView1
        private ListViewItem activeItem = null;
        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            activeItem = listView1.GetItemAt(e.X, e.Y);
        }

        private void listView1_MouseMove(object sender, MouseEventArgs e)
        {
            if (activeItem == null || e.Button != MouseButtons.Left) return;
            dragItem = new LayoutControlItem();
            InputItemInfo item = Inputs.Single(o=>o.Text==activeItem.Text);

            dragItem.Name = "lci" + item.ID;
            dragItem.Control = UIHelper.GetControlByType(item.CtrlType);// (Control)TypeCreator.CreateInstance(item.CtrlType);
            dragItem.Control.Name = "txt" + item.ID;
            dragItem.Text = activeItem.Text;
            listView1.DoDragDrop(dragItem, DragDropEffects.Copy);

            listView1.Items.Remove(activeItem);
        }

        private void listView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            e.UseDefaultCursors = false;
        }

#endregion

        #region IDragManager

        LayoutControlItem dragItem = null;
        LayoutControlItem IDragManager.DragItem { get { return dragItem; } set { dragItem = value; } }
        void IDragManager.SetDragCursor(DragDropEffects e) { SetDragCursor(e); }

        #endregion

        private Control listBoxItem;
        private void lbItems_MouseDown(object sender, MouseEventArgs e)
        {
            //listBoxItem = lbItems.GetChildAtPoint(e.Location);
        }

        private void lbItems_MouseMove(object sender, MouseEventArgs e)
        {
            if (listBoxItem == null || e.Button !=MouseButtons.Left) return;
            dragItem = new LayoutControlItem();
            dragItem.Name = "lci" + listBoxItem.Name;
            dragItem.Control = new TextBox();
            dragItem.Control.Name = "txt" + listBoxItem.Name;
            dragItem.Text = listBoxItem.Text;
            //lbItems.DoDragDrop(dragItem, DragDropEffects.Copy);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.FormSetting == null)
            {
                this.FormSetting = new FormsInfo();
            }
            this.FormSetting.ID = "test" + new Random().Next(1000);
            this.FormSetting.Text = "test";
            this.FormSetting.Controls = this.formDesigner1.InputItems;
            this.FormSetting.Layout = this.formDesigner1.LayoutXml;
            this.FormSetting.Insert();
            //this.formDesigner1
            //this.FormSetting.Controls
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
                return;

            FormsInfo fi = new FormsInfo();
            fi.ID = this.textBox1.Text.Trim();
            if (fi.SelectByKeys())
            {
                this.formDesigner1.LayoutXml = fi.Layout;
            }
        }
    }
}
